<template>
  <!-- vaif="value" -->
  <!-- 关注 -->
  <van-button
    v-if="isFollowed"
    class="follow-btn"
    round
    size="small"
    @click="onFollow"
    :loading="loading"
    >已关注</van-button
  >
  <van-button
    v-else
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    :loading="loading"
    @click="onFollow"
    >关注</van-button
  >
</template>

<script>
import { addFollow, deleteFollow } from "@/api/user";

export default {
  name: "FollowUser",
  // model: {
  //   prop: "isFollowed",
  //   event: "update-is_followed",自定义v-model的value和inpt事件
  // },
  props: {
    isFollowed: {
      type: Boolean,
      required: true,
    },
    // value: {
    //   type: Boolean,
    //   required: true,
    // },
    userId: {
      type: [Number, Object, String],
      required: true,
    },
  },
  data() {
    return {
      loading: false,
    };
  },
  methods: {
    async onFollow() {
      this.loading = true; //展示关注按钮的状态
      try {
        if (this.isFollowed) {
          // 已关注 取消关注
          await deleteFollow(this.userId);
          // console.log(data);
          // this.article.is_followed=false
        } else {
          await addFollow(this.userId);
          // console.log(data);
          // this.article.is_followed=true
        }
        // 更新视图状态
        // this.article.is_followed = !this.article.is_followed;
        this.$emit("update-is_followed", !this.article.is_followed);
        // this.$emit("input", !this.value);
      } catch (err) {
        let message = "操作失败";
        if (err.response && err.response.status === 400) {
          message = "不能自我关注哦";
        }
        console.log(message);
        // 无关注  添加关注
      }

      this.floading = false;
    },
  },
  //   封装组件
  // 1.创建vue文件
};
</script>

<style>
</style>